package com.xzk.ExpressInnWeb.controller.courier;

import com.xzk.ExpressInnWeb.bean.*;
import com.xzk.ExpressInnWeb.service.CourierService;
import com.xzk.ExpressInnWeb.util.CourierUtil;
import com.xzk.ExpressInnWeb.util.JsonUtil;
import com.xzk.ExpressInnWeb.web.mvc.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

public class CourierController {

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        Map<String, Integer> values = CourierService.consoleCourier();
        Message msg = new Message();
        if (values.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(values);
        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        int offset = Integer.parseInt(req.getParameter("offset"));
        int limit = Integer.parseInt(req.getParameter("limit"));
        List<BootstrapTableCourier> list = CourierUtil.transferCourierList(CourierService.findAllCourier(true,offset,limit));
        int total = CourierService.consoleCourier().get("size");
        ResultData<BootstrapTableCourier> data = new ResultData<>();
        data.setRows(list);
        data.setTotal(total);
        return JsonUtil.toJSON(data);
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        Message msg = new Message();
        boolean insert = CourierService.insertCourier(new Courier(
                req.getParameter("couriername"),
                req.getParameter("courierphone"),
                req.getParameter("personalid"),
                req.getParameter("password")
        ));
        if (insert)
            msg.setStatus(0);
        else
            msg.setStatus(-1);
        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/courier/search.do")
    public String search(HttpServletRequest req, HttpServletResponse resp){
        Courier courier = CourierService.findCourierByPhoneNum(req.getParameter("staffphone"));
        Message msg = new Message();
        if (courier==null) {
            msg.setStatus(-1);
            msg.setResult("手机号不存在");
        } else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(courier);
        }
        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        Message msg = new Message();
        Courier newCourier = new Courier(
                req.getParameter("couriername"),
                req.getParameter("staffphone"),
                req.getParameter("personalid"),
                req.getParameter("password")
        );
        boolean update = CourierService.updateCourier(id,newCourier);
        if (update)
            msg.setStatus(0);
        else
            msg.setStatus(-1);
        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        Message msg = new Message();
        boolean delete = CourierService.deleteCourier(id);
        if (delete) {
            msg.setStatus(0);
            msg.setResult("删除成功！");
        } else {
            msg.setStatus(-1);
            msg.setResult("删除失败！");
        }
        return JsonUtil.toJSON(msg);
    }

}
